<?php
namespace app\mobile\controller;

use app\model\Theme; 
use app\model\Worker; 

class WMyinfo extends Base
{
    public function W_myinfo()
    {
global $_G, $_P;
$fans = $this->checkuser();
$themeid = intval($_P['themeid']);
if($themeid < 0){
	return_msg('管理后台地址不正确');
}
$system=$_G['system'];
$theme = Theme::getThemeById($themeid);
$config = 	Theme::getConfig($themeid);
if(empty($theme['id'])){
	return_msg('管理后台地址不正确');
}
$myworker = Worker::getThemeWorkerByOpenidNew($_G['openid'],$themeid);
if(!($system['ruanjian']!=3 || $config['showshop']==0 || ($system['ruanjian']==3 && $config['showshop']==$myworker['company']))){
	$myworker = "";
}
if (empty($myworker)) {
	return_msg('您不是员工,无权访问');
}
$msg = Worker::gethoutaistats();
if (!empty($msg)) {
	return_msg($msg);
}
$do = 'myinfo';


//读取顶部个人信息
$shop=db_find("SELECT * FROM ".tablename(t_shangjia)." WHERE id= '{$myworker['company']}'");
$shopname=$shop['name'];

$team=db_find("SELECT * FROM ".tablename(t_team)." WHERE id= '{$myworker['team']}'");
$teamname=$team['name'];


//*****************//
//更新数据统计
//*****************//

	if (date("H")>=intval($config['todaytime'])){
		$todaytime = date("Y-m-d") . " " . intval($config['todaytime']) . ":00:00";
	}else{
		$todaytime = date("Y-m-d",strtotime("-1 day")) . " " . intval($config['todaytime']) . ":00:00";
	}
	$todaytime = strtotime($todaytime);

	$sql = 'SELECT * FROM ' . tablename(t_worker) . ' WHERE status=1 and themeid = '.$themeid.' ORDER BY uid  ';
	$list = db_select($sql, $params);
	foreach ($list as $index =>$row){
		$paycount = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
				WHERE status > 0 and workerid = '{$row['uid']}' and themeid = '{$themeid}' " );

		$unpaidcount = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
				WHERE status = 0 and workerid = '{$row['uid']}' and themeid = '{$themeid}' " );
		$paycounttoday = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
				WHERE status > 0 and workerid = '{$row['uid']}' and addtime>'{$todaytime}' and themeid = '{$themeid}' " );
		$unpaidcounttoday = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
				WHERE status = 0 and workerid = '{$row['uid']}' and addtime>'{$todaytime}' and themeid = '{$themeid}'" );

		$directcount = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
				WHERE status > 0 and fromuid = '{$row['uid']}' and themeid = '{$themeid}' and workerid = '{$row['uid']}'" );
		$directcounttoday = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
				WHERE status > 0 and fromuid = '{$row['uid']}' and addtime>'{$todaytime}' and themeid = '{$themeid}' and workerid = '{$row['uid']}'" );

		// $commcount = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_comm ) . "
		// 		WHERE status = 1 and uid = '{$row['uid']}' and themeid = '{$themeid}' " );

		// $commsum = db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename ( t_comm ) . "
		// 		WHERE status = 1 and uid = '{$row['uid']}' and themeid = '{$themeid}' " );

		// $commfail = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_comm ) . "
		// 		WHERE status = 0 and uid = '{$row['uid']}' and themeid = '{$themeid}' " );

		// $directsum = db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename ( t_record ) . "
		// 		WHERE status > 0 and fromuid = '{$row['uid']}' and themeid = '{$themeid}' and workerid = '{$row['uid']}'" );
		// $paysum = db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename ( t_record ) . "
		// 		WHERE status > 0 and workerid = '{$row['uid']}' and themeid = '{$themeid}' " );


		$data = array (
			'paycount' => $paycount,
			'directcount' => $directcount,
			'commcount'=> $commcount,
			'commsum'  => $commsum,
			'commfail' => $commfail,
			'directsum'  => $directsum,
			'paysum' => $paysum,
			'unpaidcount' => $unpaidcount,
			'paycounttoday'=> $paycounttoday,
			'unpaidcounttoday'  => $unpaidcounttoday,
			'directcounttoday'  => $directcounttoday,
		);
		db_update (t_worker, $data, array ('openid' => $row['openid'],'themeid' => $themeid) );

	}

//更新业绩数据
$my = Worker::getThemeWorkerByOpenidNew($_G['openid'],$themeid);
if ($my['level'] == '1'){	
	$shenfen="超级管理员";
	$teampaycount = db_fetchcolumn ( 'SELECT sum(paycount)  FROM ' . tablename ( t_worker ) . "
		WHERE  themeid = '{$themeid}' " );
	$teampaycounttoday = db_fetchcolumn ( 'SELECT sum(paycounttoday) FROM ' . tablename ( t_worker ) . "
		WHERE  themeid = '{$themeid}' " );
	$noworker = intval(db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . " as r
		left join " . tablename ( t_worker ) . " as w on r.workerid = w.uid and r.themeid = w.themeid
		WHERE r.themeid ='{$themeid}' and r.status > 0 and r.workerid = 0 " . $tiaojian . "" ));
	$teampaycount = $teampaycount + $noworker;
	$teamtips ="所有";
}elseif ($my['level'] == '2'){	
	$shenfen="商家管理员";
	$teampaycount = db_fetchcolumn ( 'SELECT sum(paycount)  FROM ' . tablename ( t_worker ) . "
		WHERE company=" . $my['company'] . " and themeid = '{$themeid}' " );
	$teampaycounttoday = db_fetchcolumn ( 'SELECT sum(paycounttoday) FROM ' . tablename ( t_worker ) . "
		WHERE company=" . $my['company'] . " and themeid = '{$themeid}' " );
	$teamtips ="商家";
}else{	
	$shenfen="员工";
	$teampaycount = db_fetchcolumn ( 'SELECT sum(paycount)  FROM ' . tablename ( t_worker ) . "
		WHERE team = '{$my['team']}' and themeid = '{$themeid}' " );
	$teampaycounttoday = db_fetchcolumn ( 'SELECT sum(paycounttoday) FROM ' . tablename ( t_worker ) . "
		WHERE team = '{$my['team']}' and themeid = '{$themeid}' " );
	$teamtips ="团队";
}
if (empty($myworker)) {
	return_msg('您不是员工,无权访问');
}
$msg = Worker::gethoutaistats();
if (!empty($msg)) {
	return_msg($msg);
}
$shenfen='【'.$shenfen.'】';

if ($my['level'] > 1 ){
	$tiaojian = " and w.company=" . $my['company'] ;
}
$sql = 'SELECT w.team,sum(w.paycounttoday) as today ,sum(w.paycount) as allpay,
			sum(w.scansum) as scansum , sum(w.forward) as forward, t.name FROM ' . tablename(t_worker) . ' as w
		left join ' . tablename(t_team) . ' as t on w.team = t.id
			WHERE  w.themeid = '.$themeid.$tiaojian.' 
			group by w.team
			ORDER BY t.groupid';
$team_all = db_select($sql);


if ($my['level'] == 1 ){
	foreach ($team_all as $index => $item) {
		$sql = 'SELECT * FROM ' . tablename(t_worker) . ' as w
				WHERE w.team=' . $item['team'] . ' and w.themeid = '.$themeid.'
				ORDER BY w.paycounttoday desc,w.paycount desc';
		$team_list = db_select($sql);
		$team_all[$index]['list']=$team_list;
		if (empty($team_all[$index]['name'])){
			$team_all[$index]['name']='无名队';
		}
	}
}elseif ($my['level'] == 2 ){
	foreach ($team_all as $index => $item) {
		$sql = 'SELECT * FROM ' . tablename(t_worker) . ' as w
				WHERE w.company=' . $my['company'] . ' and w.team=' . $item['team'] . ' and w.themeid = '.$themeid.'
				ORDER BY w.paycounttoday desc,w.paycount desc';
		$team_list = db_select($sql);
		$team_all[$index]['list']=$team_list;
	}
}else{
	$sql = 'SELECT * FROM ' . tablename(t_worker) . ' as w
		WHERE w.company=' . $my['company'] . ' and w.team=' . $my['team'] . ' and w.themeid = '.$themeid.'
		ORDER BY w.paycounttoday desc,w.paycount desc';
	$team_list = db_select($sql);
}

if ($config['ishexiao']==1 && $my['level'] < 3){
	$sql = 'SELECT c.id as cid,sum(w.paycounttoday) as today ,sum(w.paycount) as allpay,
			sum(w.scansum) as scansum , sum(w.forward) as forward , c.name FROM ' . tablename(t_worker) . ' as w
			left join ' . tablename(t_shangjia) . ' as c on w.company = c.id
				WHERE w.themeid = '.$themeid.' and c.state=1 and c.state=1
				group by w.company
				ORDER BY today desc ,allpay desc';
	$company_all = db_select($sql);
	foreach ($company_all as $index => $item) {
		if (!empty($item['cid'])){
			$sql = 'SELECT * FROM ' . tablename(t_shangjia) . ' as w
					WHERE w.id=' . $item['cid'] ;
			$list = db_find($sql);
			$company_all[$index]['forward'] = $company_all[$index]['forward'] + $list['xunizhuanfa'];
			$company_all[$index]['scansum'] = $company_all[$index]['scansum'] + $list['xuniliulan'];
			$company_all[$index]['today'] = $company_all[$index]['today'] + $list['xunijinri'];
			$company_all[$index]['allpay'] = $company_all[$index]['allpay'] + $list['xunichengjiao'];
		}
	}

}

if($_G['companyname'] =='新飞驰软件'){
	$return_clicent = get_defined_vars();
return view('/fcworker/fc_myinfo' , $return_clicent);
}else{
	$return_clicent = get_defined_vars();
return view('/yxworker/w_myinfo' , $return_clicent);
}











}
}